<?php

namespace App\Http\Controllers;

use App\Helper\Mongo\Mongo;
use App\Jobs\ExampleJob;
use App\Model\Bll\ArticleCateBll;
use App\Model\Bll\UserBll;
use App\Model\Dao\MongoMember;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;

class DemoController extends Controller
{

    private $user, $articleCate, $request;
    public function __construct(Request $request, UserBll $userBll, ArticleCateBll $articleCateBll)
    {
        //
        $this->user = $userBll;
        $this->articleCate = $articleCateBll;
        $this->request = $request;
        parent::__construct($request);
    }

    /**
     * redis demo
     */
    public function index()
    {

        // redis test
        $redis = Redis::connection('share_3');
//        $status = $redis->set('name','葛有文');

//        $redis->expire('name', 100);
//        var_dump($status);
        //队列
        $redis->rpush('MQ', time());
//        echo $redis->lpop('MQ');
//        print_r(unserialize($redis->lpop('MQ')));
        exit;
    }


    /**
     * mongodb demo
     * @return \Illuminate\Http\JsonResponse
     */
    public function mongo()
    {
//        $data = [
//            'username' => 'gywbank',
//            'email'    => '690591423@qq.com',
//        ];
//        for($i = 1; $i <= 10000; $i++)
//        {
//            $data['id'] = $i;
//            $data['location'] = [
//              rand(80,121)+0.234322,
//              rand(10,30)+0.443452
//            ];
//            $this->user->insert($data);
//            echo $i;
//        }
//        $list = $this->user->getAll();
//        return response()->json($list);
        $mongo = new MongoMember();
        $latitude = [110.234322, 17.443452];
        $option = [
            'geoNear'            => 'member',
            'near'               => $latitude,
            'distanceMultiplier' => 6378137,
            'spherical'          =>  true,
            'num'                => 20,
            'query'              => ['id' => 101],
        ];
        $list = $mongo->distance($option);
        return $this->respond(200, $list);
    }

    /**
     * mysql demo
     * @return \Illuminate\Http\JsonResponse
     */
    public function mysql()
    {
        $list = $this->articleCate->getAll();
        return $this->respond($list);
    }

    /**
     * email demo
     * @return \Illuminate\Http\JsonResponse
     */
    public function mail()
    {
        $user = [
            'email' => '690591423@qq.com',
        ];
        try
        {
            Mail::send('mails.welcome', ['key' => 'value111'], function($mail) use($user)
            {
                $mail->to($user['email'])->subject('测试邮件!');
            });
        } catch(\Exception $e)
        {
            return response()->json([$e->getMessage()]);
        }
        return response()->json(['ok']);

    }


    /**
     * 数据验证demo
     */
    public function verify()
    {
       $this->validate($this->request, [
            'title' => 'required|numeric|max:20',
            'body'  => 'required|numeric',
            'username'=> 'required|unique:mysql.admin,username'
        ], [
            'title.required' => '标题必须',
            'title.numeric'  => '标题必须是整数',
            'title.max'      => '标题不能超过20',
            'body.required'  => '内容必须',
            'username.unique'=> '用户名已经被占用'
        ]);
        $pam = array_filter($this->request->all());
        unset($pam['_sign']);
        print_r($pam);
    }

    //执行脚本
    public function task()
    {
        echo $this->request->lang;
//        echo $this->request->ip();
//        Cache::forever('age1', 200);
//        Cache::put('age2', 200, 1);
//        echo Cache::get('age1');
//        echo Cache::get('age2');
//        echo Cache::forget('age1');
//        dispatch(new ExampleJob);
//        Queue::push(new ExampleJob);
        exit;
    }

}
